﻿using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using ProtectThePieWP7.AdditionalClasses;
using Microsoft.Xna.Framework.Input;
using ProtectThePieWP7.GeneralClasses;
using ThreeThingGame;

namespace ProtectThePie
{
    public enum MainMenuState
    {
        mainMenu,
        options
    }

    public class MainMenu
    {
        private MainMenuState state;
        private Point fieldSize;
        private GameplayBackground background;
        private MenuSystem mainMenu;
        private MenuSystem optionsMenu;

        public MainMenu(Point fieldSize)
        {
            this.state = MainMenuState.mainMenu;
            this.fieldSize = fieldSize;
            this.background = new GameplayBackground(fieldSize);

            //Adding and configuring menus
            mainMenu = new MenuSystem(Game1.menuClick, Game1.menuTexture);
            optionsMenu = new MenuSystem(Game1.menuClick, Game1.menuTexture);

            mainMenu.AddMenuEntry("New Game", MenuEntryType.link);
            mainMenu.AddMenuEntry("Options", MenuEntryType.link);

            optionsMenu.AddMenuEntry("Sound: On", MenuEntryType.lever);
            optionsMenu.AddMenuEntry("Buzz: On", MenuEntryType.lever);
            optionsMenu.AddMenuEntry("Back", MenuEntryType.link);
        }

        public void UpdateMenuEntries(MySoundEngine soundEngine)
        {
            if (Game1.soundEngine.IsSoundOn()) optionsMenu.ChangeMenyEntryText("Sound: Off", "Sound: On");
            else optionsMenu.ChangeMenyEntryText("Sound: On", "Sound: Off");

            if (Game1.soundEngine.IsVibrationTurnedOn()) optionsMenu.ChangeMenyEntryText("Buzz: Off", "Buzz: On");
            else optionsMenu.ChangeMenyEntryText("Buzz: On", "Buzz: Off");          
        }

        public bool Draw(SpriteBatch spriteBatch)
        {  
            //Draw Background
            //background.Draw(spriteBatch);

            //Draw Menu Entries
            switch (state)
            {
                case MainMenuState.mainMenu:
                    mainMenu.Draw(spriteBatch);
                    break;
                case MainMenuState.options:
                    optionsMenu.Draw(spriteBatch);
                    break;
            }
            
            return true;
        }

        public bool Update(TouchCollection touches)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Game1.exit = true;

            //Update Menu
            switch (state)
            {
                case MainMenuState.mainMenu:
                    switch (mainMenu.Update(touches))
                    {
                        case "New Game":                           
                            Game1.state = GameState.NewGame;
                            break;
                        case "Options":
                            state = MainMenuState.options;
                            break;
                        case "About":
                            Game1.state = GameState.About;
                            break;
                        case "":
                            break;
                    }
                    break;
                case MainMenuState.options:
                    //Update options menu
                    UpdateMenuEntries(Game1.soundEngine);

                    switch (optionsMenu.Update(touches))
                    {
                        case "Sound: On":
                            Game1.soundEngine.TurnSoundOff();
                            optionsMenu.ChangeMenyEntryText("Sound: On", "Sound: Off");
                            break;
                        case "Sound: Off":
                            Game1.soundEngine.TurnSoundOn();
                            optionsMenu.ChangeMenyEntryText("Sound: Off", "Sound: On");
                            break;
                        case "Buzz: On":
                            Game1.soundEngine.TurnVibrationOff();
                            optionsMenu.ChangeMenyEntryText("Buzz: On", "Buzz: Off");
                            break;
                        case "Buzz: Off":
                            Game1.soundEngine.TurnVibrationOn();
                            optionsMenu.ChangeMenyEntryText("Buzz: Off", "Buzz: On");
                            break;
                        case "Back":
                            state = MainMenuState.mainMenu;
                            break;
                        case "":
                            break;
                    }
                    break;
            }

            return true;
        }
    }
}
